package org.tech4j.common.core.model.response;

import java.io.Serial;
import java.util.Collection;
import java.util.List;

/**
 * @program: tech4j-common
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/1 21:41
 **/
public class PageResponse<T> extends MultiResponse<T> {
    @Serial
    private static final long serialVersionUID = -4206807802709008444L;

    /**
     * 当前页
     */
    private int currentPage;
    /**
     * 每页结果数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 总数
     */
    private int total;

    public static <T> PageResponse<T> of(Collection<T> data, int total, int pageSize, int currentPage) {
        PageResponse<T> pageResponse = new PageResponse<>();
        pageResponse.setSuccess(true);
        pageResponse.setData(data);
        pageResponse.setTotal(total);
        pageResponse.setPageSize(pageSize);
        pageResponse.setCurrentPage(currentPage);
        pageResponse.setTotalPage((pageSize + total - 1) / pageSize);
        return pageResponse;
    }

    public static <T> PageResponse<T> of(Collection<T> data, int total, int pageSize, int currentPage, int totalPages) {
        PageResponse<T> pageResponse = new PageResponse<>();
        pageResponse.setSuccess(true);
        pageResponse.setData(data);
        pageResponse.setTotal(total);
        pageResponse.setPageSize(pageSize);
        pageResponse.setCurrentPage(currentPage);
        pageResponse.setTotalPage(totalPages);
        return pageResponse;
    }

    public static <T> PageResponse<T> fail(String errorCode, String errorMessage) {
        PageResponse<T> pageResponse = new PageResponse<>();
        pageResponse.setSuccess(false);
        pageResponse.setResponseCode(errorCode);
        pageResponse.setResponseMessage(errorMessage);
        return pageResponse;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}
